﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using System.Text;

namespace OA.Controllers
{
    public class ConsumeSolutionController : Controller
    {
        //
        // GET: /ConsumeSolution/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ConsumeSolution()
        {
            return View();
        }
       
        public ActionResult LoadSolution()
        {
            consumeSolution CS = new consumeSolution();
            consumeSolutionManager CSManager = new consumeSolutionManager();
            List<consumeSolution> list = CSManager.GetAll().ToList();
            cardTemplates cts = new cardTemplates();
            cardTemplatesManager ctsManager = new cardTemplatesManager();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count(); i++)
            {
                sb.Append("<tr ondblclick=\"editSolution(" + list[i].id + ")\" onclick=\"selectSolution(" + list[i].id + ")\" onmouseover=\"this.style.backgroundColor='#BCD2EE';cursor='default'\" onmouseout=\"this.style.backgroundColor='white'\">");
                sb.Append("<td>"+list[i].id+"</td>");
                sb.Append("<td>" + list[i].solutionName + "</td>");
                sb.Append("<td>" + ctsManager.GetByID(Convert.ToInt32(list[i].cardTemplateId)).name + "</td>");
                sb.Append("<td>" +list[i].consumeSolution1+ "</td>");
                sb.Append("<td>" + list[i].savingSolution+ "</td>");
                sb.Append("<td>" + list[i].discountSolution+ "</td>");
                int nextId = Convert.ToInt32(list[i].nextLevelId);
                if (nextId != 0)
                {
                    sb.Append("<td>" + CSManager.GetByID(nextId).solutionName+ "</td>");
                }
                else
                {
                    sb.Append("<td>最高</td>");
                }
                sb.Append("<td>" + Convert.ToDecimal(list[i].levelUp).ToString("#0.00") + "</td>");
                sb.Append("<td>" + list[i].integralday + "</td>");
                sb.Append("</tr>");
            }

            return Content(sb.ToString());
        }
         
        //消费方案
        public ActionResult ConsumeSolutionSetting()
        {
            string Id = System.Web.HttpContext.Current.Request.QueryString["Id"];
            consumeSolution CS = new consumeSolution();
            consumeSolutionManager CSManager = new consumeSolutionManager();
            cardTemplates cts = new cardTemplates();
            cardTemplatesManager ctsManager = new cardTemplatesManager();
            if (!string.IsNullOrEmpty(Id))
            {
                CS = CSManager.GetByID(Convert.ToInt32(Id));
                ViewData["solutionName"] = CS.solutionName;
                List<cardTemplates> cardtep = ctsManager.GetAll().ToList();
                List<SelectListItem> listTep = new List<SelectListItem>();
                for (int i = 0; i < cardtep.Count(); i++)
                {
                    if (cardtep[i].id == Convert.ToInt32(Id))
                    {
                        listTep.Add(new SelectListItem { Text = cardtep[i].name, Value = cardtep[i].id.ToString(), Selected = true });
                    }
                    else
                    {
                        listTep.Add(new SelectListItem { Text = cardtep[i].name, Value = cardtep[i].id.ToString() });
                    }
                }
                ViewData["tep"] = listTep;
                List<SelectListItem> list = new List<SelectListItem>();
                string[] consume = CS.consumeSolution1.ToString().Split('|');
                for (int i = 0; i < consume.Count() - 1; i++)
                {
                    list.Add(new SelectListItem { Text = consume[i], Value = consume[i] });
                }
                ViewData["select1"] = list;
                string[] con = consume[0].Split(',');
                ViewData["money"] = con[0];
                ViewData["point"] = con[1];
                List<SelectListItem> listSaving = new List<SelectListItem>();
                string[] savingsolutin = CS.savingSolution.ToString().Split('|');
                for (int i = 0; i < savingsolutin.Count() - 1; i++)
                {
                    listSaving.Add(new SelectListItem { Text = savingsolutin[i], Value = savingsolutin[i] });
                }
                ViewData["select2"] = listSaving;
                string[] saving = savingsolutin[0].Split(',');
                ViewData["Cmoney"] = saving[0];
                ViewData["Smoney"] = saving[1];
                ViewData["discount"] = CS.discountSolution;
                List<string> listSoluntionName = CSManager.getAllSoluntionName().ToList();
                consumeSolution NextCs = CSManager.GetByID(Convert.ToInt32(CS.nextLevelId));
                List<SelectListItem> listNextLevel = new List<SelectListItem>();
                for (int i = 0; i <=listSoluntionName.Count(); i++)
                {
                    if (CS.nextLevelId > 0)
                    {

                        if (i == listSoluntionName.Count())
                        {
                            listNextLevel.Add(new SelectListItem { Text = "最高", Value = "最高" });
                            break;
                        }
                        if (NextCs.solutionName == listSoluntionName[i])
                        {
                            listNextLevel.Add(new SelectListItem { Text = listSoluntionName[i], Value = listSoluntionName[i], Selected = true });
                            ViewData["consume"] = Math.Round(Convert.ToDecimal(CS.levelUp), 2).ToString();
                        }
                        else
                        {
                            listNextLevel.Add(new SelectListItem { Text = listSoluntionName[i], Value = listSoluntionName[i] });
                        }
                       
                    }
                    else
                    {
                        if (i == listSoluntionName.Count())
                        {
                            listNextLevel.Add(new SelectListItem { Text = "最高", Value = "最高", Selected = true });
                            ViewData["consume"] = Math.Round(Convert.ToDecimal(CS.levelUp), 2).ToString();
                            break;
                        }
                        listNextLevel.Add(new SelectListItem { Text = listSoluntionName[i], Value = listSoluntionName[i] });

                    }
                }
                ViewData["nextLevel"] = listNextLevel;
                ViewData["day"] = CS.integralday;
                return View();
            }
            else
            {
                List<cardTemplates> cardtep = ctsManager.GetAll().ToList();
                List<SelectListItem> listTep = new List<SelectListItem>();
                for (int i = 0; i < cardtep.Count(); i++)
                {
                        listTep.Add(new SelectListItem { Text = cardtep[i].name, Value = cardtep[i].id.ToString() });
                }
                ViewData["tep"] = listTep;
                List<string> listSoluntionName = CSManager.getAllSoluntionName().ToList();
                List<SelectListItem> listNextLevel = new List<SelectListItem>();
                for (int i = 0; i <= listSoluntionName.Count(); i++)
                {
                    if (i == listSoluntionName.Count())
                    {
                        listNextLevel.Add(new SelectListItem { Text = "最高", Value = "最高", Selected = true });
                        break;
                    }
                    listNextLevel.Add(new SelectListItem { Text = listSoluntionName[i], Value = listSoluntionName[i] });
                }
                ViewData["nextLevel"] = listNextLevel;
                ViewData["select1"] = new List<SelectListItem>();
                ViewData["select2"] = new List<SelectListItem>();
                return View();
            }
             
        }

        //保存
        public ActionResult ConsumeSolutionSave(FormCollection fc)
        {
            string Id = fc["Id"];
            string consumeStr = fc["consumeStr"];
            string savingStr = fc["savingStr"];
            string solutionName = fc["solutionName"];
            string discount = fc["discount"];
            string consume = fc["consume"];
            int day =Convert.ToInt32(fc["day"]);
            int tep =Convert.ToInt32(fc["tep"]);
            string nextLevel = fc["nextLevel"];
            consumeSolution CS = new consumeSolution();
            consumeSolutionManager CSManager = new consumeSolutionManager();
            cardTemplates cts = new cardTemplates();
            cardTemplatesManager ctsManager = new cardTemplatesManager();
            if (string.IsNullOrEmpty(Id)|| Id=="null")
            {
                CS.solutionName = solutionName;
                CS.cardTemplateId = tep;
                CS.consumeSolution1 = consumeStr;
                CS.savingSolution = savingStr;
                CS.discountSolution = discount;
                if (nextLevel != "最高")
                {
                    CS.nextLevelId = CSManager.getSolutionBySolutionName(nextLevel).id;
                }
                else 
                {
                    CS.nextLevelId = 0;
                }
                CS.levelUp =Convert.ToDecimal(consume);
                CS.integralday = day;
                CSManager.Add(CS);
                CSManager.Update();
                return Content("添加成功");
            }
            else
            {
                CS = CSManager.GetByID(Convert.ToInt32(Id));
                if (CS != null)
                {
                    CS.solutionName = solutionName;
                    CS.cardTemplateId = tep;
                    CS.consumeSolution1 = consumeStr;
                    CS.savingSolution = savingStr;
                    CS.discountSolution = discount;
                    if (nextLevel != "最高")
                    {
                        CS.nextLevelId = CSManager.getSolutionBySolutionName(nextLevel).id;
                    }
                    else
                    {
                        CS.nextLevelId = 0;
                    }
                    CS.levelUp = Convert.ToDecimal(consume);
                    CS.integralday = day;
                    CSManager.Update();
                    return Content("更新成功");
                }
              
            }
            return View();
        }

        //删除方案
        public ActionResult delSolution(FormCollection fc)
        {
            string Id = fc["Id"];
            if (!string.IsNullOrEmpty(Id) && Id != "null")
            {
                consumeSolution CS = new consumeSolution();
                consumeSolutionManager CSManager = new consumeSolutionManager();
                int id = Convert.ToInt32(Id);
                consumeSolution nextCs= CSManager.getSolutinByNextLevelId(id);
                memberInfoManager miManager = new memberInfoManager();
                if (nextCs != null)
                {
                    return Content("级联");
                }
                if (miManager.getNumByCardType(id) > 0)
                {
                    return Content("会员卡号");
                }
                CSManager.delSolutionById(id);
                return Content("删除成功");
            }
            return Content("删除失败");
        }
    }
}
